package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.appuser.entity.TbUserSearchHistory;
import org.jeecg.modules.appuser.mapper.TbUserSearchHistoryMapper;
import org.jeecg.modules.appuser.service.ITbUserSearchHistoryService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 用户搜索历史表
 * @Author: jeecg-boot
 * @Date: 2020-05-20
 * @Version: V1.0
 */
@Service
public class TbUserSearchHistoryServiceImpl extends ServiceImpl<TbUserSearchHistoryMapper, TbUserSearchHistory> implements ITbUserSearchHistoryService {

    @Override
    public Result<?> addHistorySearch(String user_id, String serarchName) {
        TbUserSearchHistory tbUserSearchHistory = new TbUserSearchHistory();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("search_name", serarchName);
        List<TbUserSearchHistory> tbUserSearchHistoryList = this.list(queryWrapper);
        if (null == tbUserSearchHistoryList || tbUserSearchHistoryList.size() == 0) {
            tbUserSearchHistory.setSearchName(serarchName);
            tbUserSearchHistory.setState(1);
            tbUserSearchHistory.setUserId(user_id);
            this.save(tbUserSearchHistory);
        }
        return Result.ok("添加成功");
    }

    @Override
    public Result<?> deleteHistorySearchList(String user_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        if (this.remove(queryWrapper)) {
            return Result.ok("删除成功");
        }
        return Result.ok("删除完成");
    }

    @Override
    public Result<?> getHistorySearchList(String user_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        List<TbUserSearchHistory> tbUserSearchHistoryList = this.list(queryWrapper);

        return Result.ok(tbUserSearchHistoryList);
    }
}
